package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author tantao
 * @create 2020-04-11 22:35
 * @desc
 **/
@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;


    //通过id进行查询
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable Long id) {
        Payment payment = paymentService.selectPaymentById(id);
        log.info("****查询结果为：****" + payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        }
        return new CommonResult(444, "查询失败,id:" + id, null);
    }

    //通过id和name进行查询
    @GetMapping("/payment/get/{id}/{name}")
    public CommonResult getPaymentById(@PathVariable("id") Long id, @PathVariable("name") String name) {
        System.out.println("获取到的数据id:" + id + ",name:" + name);
        Payment payment = paymentService.selectPaymentById(id);
        log.info("****查询结果为：****" + payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        }
        return new CommonResult(444, "查询失败,id:" + id, null);
    }

    //通过id进行查询
    @GetMapping("/payment/get01")
    public CommonResult getPaymentById01(@RequestParam("id") Long id) {
        Payment payment = paymentService.selectPaymentById(id);
        log.info("****查询结果为：****" + payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        }
        return new CommonResult(444, "查询失败,id:" + id, null);
    }

    //通过id进行查询
    @GetMapping("/payment/get02")
    public CommonResult getPaymentById02(@RequestParam("id") Long id,
                                         @RequestParam("name") String name) {
        System.out.println("获取到的数据id:" + id + ",name:" + name);
        Payment payment = paymentService.selectPaymentById(id);
        log.info("****查询结果为：****" + payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        }
        return new CommonResult(444, "查询失败,id:" + id, null);
    }

    //通过id进行查询
    @GetMapping("/payment/get03/{id}")
    public CommonResult getPaymentById03(@PathVariable("id") Long id,
                                         @RequestParam("name") String name) {
        System.out.println("获取到的数据id:" + id + ",name:" + name);
        Payment payment = paymentService.selectPaymentById(id);
        log.info("****查询结果为：****" + payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        }
        return new CommonResult(444, "查询失败,id:" + id, null);
    }


    //新增
    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        System.out.println("payment:" + payment);
        int result = paymentService.create(payment);
        log.info("****插入结果：****" + result);
        if (result > 0) {
            return new CommonResult(200, "新增成功！", result);
        }
        return new CommonResult(404, "新增失败！", null);
    }

    @PostMapping("/payment/create01/{id}")
    public CommonResult create01(@RequestBody Payment payment, @PathVariable("id") Long id) {
        System.out.println("payment:" + payment + ",id:" + id);
        int result = paymentService.create(payment);
        log.info("****插入结果：****" + result);
        if (result > 0) {
            return new CommonResult(200, "新增成功！", result);
        }
        return new CommonResult(404, "新增失败！", null);
    }


    @PostMapping("/payment/create02/{id}")
    public CommonResult create02(@RequestBody Payment payment, @PathVariable("id") Long id, @RequestParam("name") String name) {
        System.out.println("payment:" + payment + ",id:" + id + ",name:" + name);
        int result = paymentService.create(payment);
        log.info("****插入结果：****" + result);
        if (result > 0) {
            return new CommonResult(200, "新增成功！", result);
        }
        return new CommonResult(404, "新增失败！", null);

    }


}
